﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;

namespace QuickResizer
{
	public partial class Configuration : Form
	{
		private List<ControlRow> controlRows;
		private Resizers resizers;

		public Configuration()
		{
			InitializeComponent();

			// build collection of field controls to add in handling
			BuildControlRows();

			// load configuration xml data into memory
			ReadData();
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			try
			{
				GetValuesFromForm();
				// persist configuration xml to a file
				resizers.SaveToConfiguration();

				// perform the registry update
				if (SetRegistry())
				{
					MessageBox.Show("Configuration data saved successfully!");
				}
				else
				{
					MessageBox.Show("An error has occurred while updating the registry.  Please see the event log for details.");
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void CloseButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void GetValuesFromForm()
		{
			SetResizers();
			resizers.SubFolder = subFolder.Text;
		}

		private void SetResizers()
		{
			for (int i = 0; i < controlRows.Count; i++)
			{
				if (!string.IsNullOrWhiteSpace(controlRows[i].NameTextBox.Text))
				{
					ResizerItem item;
					if (resizers.ContainsKey(i))
					{
						item = resizers[i];
					}
					else
					{
						item = new ResizerItem();
						resizers.Add(i, item);
					}
					item.Index = i;
					item.Show = controlRows[i].ShowCheckBox.Checked;
					item.Name = controlRows[i].NameTextBox.Text;
					item.Width = Convert.ToInt32(controlRows[i].WidthTextBox.Text);
					item.Height = Convert.ToInt32(controlRows[i].HeightTextBox.Text);
					item.Prefix = controlRows[i].PrefixTextBox.Text;
					item.Suffix = controlRows[i].SuffixTextBox.Text;
					item.SubFolder = subFolder.Text;
				}
			}
		}

		/// <summary>
		/// Spawns a new process with elevated perms to update the rgeistry keys
		/// </summary>
		private bool SetRegistry()
		{
			ProcessStartInfo setRegistryProcess = new ProcessStartInfo(Application.ExecutablePath, "/s");
			setRegistryProcess.UseShellExecute = true;
			setRegistryProcess.Verb = "runas";
			Process process = new Process();
			process.StartInfo = setRegistryProcess;
			process.Start();
			process.WaitForExit();

			if (process.ExitCode != 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		/// <summary>
		/// Reads from the configuration xml and loads the values to the appropriate fields
		/// </summary>
		private void ReadData()
		{
			resizers = new Resizers();
			resizers.LoadFromConfiguration();

			SetFormFields();

			this.subFolder.Text = resizers.SubFolder;

		}

		/// <summary>
		/// Helper method for loading values into the form fields
		/// </summary>
		private void SetFormFields()
		{
			for (int i = 0; i < resizers.Count; i++)
			{
				if (controlRows.Count > i)
				{
					controlRows[i].ShowCheckBox.Checked = resizers[i].Show;
					controlRows[i].NameTextBox.Text = resizers[i].Name;
					controlRows[i].WidthTextBox.Text = resizers[i].Width.ToString();
					controlRows[i].HeightTextBox.Text = resizers[i].Height.ToString();
					controlRows[i].PrefixTextBox.Text = resizers[i].Prefix;
					controlRows[i].SuffixTextBox.Text = resizers[i].Suffix;
				}
			}
		}

		/// <summary>
		/// Adds form controls to a collection to ease processing
		/// </summary>
		private void BuildControlRows()
		{
			controlRows = new List<ControlRow>();
			controlRows.Add(new ControlRow() { ShowCheckBox = show0, NameTextBox = name0, WidthTextBox = width0, HeightTextBox = height0, PrefixTextBox = prefix0, SuffixTextBox = suffix0 });
			controlRows.Add(new ControlRow() { ShowCheckBox = show1, NameTextBox = name1, WidthTextBox = width1, HeightTextBox = height1, PrefixTextBox = prefix1, SuffixTextBox = suffix1 });
			controlRows.Add(new ControlRow() { ShowCheckBox = show2, NameTextBox = name2, WidthTextBox = width2, HeightTextBox = height2, PrefixTextBox = prefix2, SuffixTextBox = suffix2 });
		}

	}
}
